package com.wxxymaker.interceptor;

import com.wxxymaker.interceptor.annocation.AuthRequire;
import com.wxxymaker.model.entity.Admin;
import com.wxxymaker.service.AdminService;
import com.wxxymaker.utils.Const;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: Soft
 * @Date: 2018/5/7 17:07
 * @Desc: 管理员界面拦截器
 */
public class AdminInterceptor extends AuthInterceptor {
    private final static Logger log = LoggerFactory.getLogger(AdminInterceptor.class);

    @Autowired
    private AdminService adminService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler) throws Exception {
        HandlerMethod method = null;
        try {
            method = (HandlerMethod) handler;
        } catch (Exception e) {
            log.error(request.getRequestURL() + "不是spring mvc所管理的处理器，无法进行转换-> {}", e.getMessage());
            return true;
        }
        AuthRequire authRequire = method.getMethodAnnotation(AuthRequire.class);

        // 忽略掉被AuthRequire注解的处理器
        return null != authRequire ||
                this.auth(request, response, Const.A_SESSION, Const.A_COOKIE, "/admin/login",
                        adminService, Admin.class, "admin");
    }
}
